package middle.id_1_100;

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2019-2020. All rights reserved.
 * Note: 提供的缺省代码仅供参考，可自行根据答题需要进行使用、修改或删除。
 */

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

/**
 * OJ考题代码：完美答案收集
 *
 * @author 命题组
 * @since 2019-12-11
 */
public class Hua14 {
    /**
     * main入口由OJ平台调用
     */
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in, StandardCharsets.UTF_8.name());
        int questionsCount  = cin.nextInt();
        int peopleCount = cin.nextInt();
        int[][] correctRanges = new int[peopleCount][];
        for (int i = 0; i < peopleCount; i++) {
            correctRanges[i] = new int[2];
            correctRanges[i][0] = cin.nextInt();
            correctRanges[i][1] = cin.nextInt();
        }
        cin.close();

        System.out.println(getMinPeople(questionsCount , peopleCount, correctRanges));
    }

    private static int getMinPeople(int questionsCount , int peopleCount, int[][] correctRanges) {
        // 在此补充你的代码
        Arrays.sort(correctRanges, Comparator.comparingInt(a -> a[0]));
        int rightEdge = 0; //当前已凑齐的最大题目
        int cnt = 0;  // 统计结果
        int maxRight = Integer.MIN_VALUE;  // 能到达的最右边
        for (int[] range : correctRanges) {
            if (range[0] > rightEdge + 1) {  //
                rightEdge = maxRight;
                maxRight = Integer.MIN_VALUE;
                cnt++;
            }
            if (range[0] <= rightEdge + 1) {  // 能继续延续
                maxRight = Math.max(maxRight, range[1]); //合并的区间
            }
        }
        if (maxRight > rightEdge) {
            rightEdge = maxRight;
            cnt++;
        }
        return rightEdge >= questionsCount ? cnt : -1;
    }
}
